using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.DataAccessProvider;
using DTN.Core;
namespace com.dtn.deals
{
    public class tbl_NewsManager : Itbl_NewsManager
    {
        private static Itbl_NewsManager itbl_NewsManager = null;
        private tbl_NewsManager()
        {
        }
        public static Itbl_NewsManager CreateInstance()
        {
            if (itbl_NewsManager == null)
            {
                itbl_NewsManager = new tbl_NewsManager();
            }
            return itbl_NewsManager;
        }
        public int Inserttbl_News(tbl_News tbl_news)
        {
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
																new SqlParameter("@pId", SqlDbType.Int),
                                                                new SqlParameter("@partnerId", SqlDbType.Int),
																new SqlParameter("@title", SqlDbType.NVarChar),
																new SqlParameter("@desc", SqlDbType.NVarChar),
																new SqlParameter("@content", SqlDbType.NVarChar),
																new SqlParameter("@rank", SqlDbType.Int),
																new SqlParameter("@isDel", SqlDbType.TinyInt),
																new SqlParameter("@cDate", SqlDbType.DateTime),
															};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = tbl_news.pId;
            parameters[2].Value = tbl_news.PartnerID;
            parameters[3].Value = tbl_news.title;
            parameters[4].Value = tbl_news.desc;
            parameters[5].Value = tbl_news.content;
            parameters[6].Value = tbl_news.rank;
            parameters[7].Value = tbl_news.isDel;
            parameters[8].Value = tbl_news.cDate;
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Inserttbl_News", CommandType.StoredProcedure, parameters);
                tbl_news.ID = Convert.ToInt32(parameters[0].Value);
            }
            catch (Exception ex)
            {
                tbl_news.ID = -1;
            }
            return tbl_news.ID;
        }
        public bool Updatetbl_News(tbl_News tbl_news)
        {
            bool bresult = false;
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
																new SqlParameter("@pId", SqlDbType.Int),
                                                                new SqlParameter("@partnerId", SqlDbType.Int),
																new SqlParameter("@title", SqlDbType.NVarChar),
																new SqlParameter("@desc", SqlDbType.NVarChar),
																new SqlParameter("@content", SqlDbType.NVarChar),
																new SqlParameter("@rank", SqlDbType.Int),
																new SqlParameter("@isDel", SqlDbType.TinyInt),
																new SqlParameter("@cDate", SqlDbType.DateTime),
															};
            parameters[0].Value = tbl_news.ID;
            parameters[1].Value = tbl_news.pId;
            parameters[2].Value = tbl_news.PartnerID;
            parameters[3].Value = tbl_news.title;
            parameters[4].Value = tbl_news.desc;
            parameters[5].Value = tbl_news.content;
            parameters[6].Value = tbl_news.rank;
            parameters[7].Value = tbl_news.isDel;
            parameters[8].Value = tbl_news.cDate;
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Updatetbl_News", CommandType.StoredProcedure, parameters);
                bresult = true;
            }
            catch (Exception ex)
            {
                bresult = false;
            }
            return bresult;
        }
        public bool Deletetbl_News(int id)
        {
            bool bresult = false;
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            int inumOfRowEffect = 0;
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
            parameters[0].Value = id;
            try
            {
                inumOfRowEffect = dataProvider.ExecuteNonQuery("sp_Deletetbl_News", CommandType.StoredProcedure, parameters);
                bresult = true;
            }
            catch (Exception ex)
            {
                bresult = false;
            }
            return bresult;
        }
        public tbl_News Selecttbl_NewsByID(int id)
        {
            IDataReader dr;
            tbl_News tbl_news = new tbl_News();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@ID", SqlDbType.Int),
															};
            parameters[0].Value = id;
            try
            {
                dr = dataProvider.ExecuteReader("sp_Selecttbl_News", CommandType.StoredProcedure, parameters);
                while (dr.Read())
                {
                    tbl_news.ID = Convert.ToInt32(dr["ID"].ToString());
                    tbl_news.pId = Convert.ToInt32(dr["pId"].ToString());
                    tbl_news.PartnerID = Convert.ToInt32(dr["partnerId"].ToString());
                    tbl_news.title = (dr["title"].ToString());
                    tbl_news.desc = (dr["desc"].ToString());
                    tbl_news.content = (dr["content"].ToString());
                    tbl_news.rank = Convert.ToInt32(dr["rank"].ToString());
                    tbl_news.isDel = Convert.ToInt32(dr["isDel"].ToString());
                    tbl_news.cDate = Convert.ToDateTime(dr["cDate"].ToString());
                }

            }
            catch (Exception ex)
            {
                return null;
            }
            return tbl_news;
        }
        public DataTable Selecttbl_NewssDynamic(string Where, string Order)
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parameters = new SqlParameter[]{
																new SqlParameter("@WhereCondition", SqlDbType.NVarChar,500),
																new SqlParameter("@OrderByExpression", SqlDbType.NVarChar,250),
															};
            parameters[0].Value = Where;
            parameters[1].Value = Order;
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_Selecttbl_NewssDynamic", CommandType.StoredProcedure, parameters);
            }
            catch (Exception ex)
            {
                return null;
            }
            return ds.Tables["Table"];
        }
        public DataTable Selecttbl_NewssDynamicWithPage(int StartIndex, int MaxRecords, string Where)
        {
            DataSet ds = new DataSet();
            DataProviderBaseClass dataProvider = DataProvider.GetDataProvider(EnumDataProviderType.SqlClient, DataHelper.GetConnectionString());
            SqlParameter[] parrams = { new SqlParameter("@Startindex",SqlDbType.Int ){Value = StartIndex },
                                         new SqlParameter("@Maxrecords",SqlDbType.Int ){Value = MaxRecords  },
                                         new SqlParameter("@WhereCondition",SqlDbType.NVarChar,500 ){Value = Where  }
                                     };
            try
            {
                ds = dataProvider.ExecuteDataSet("sp_Selecttbl_NewsWithPagesDynamic", CommandType.StoredProcedure, parrams);
            }
            catch (Exception ex)
            {
                return null;
            }
            return ds.Tables["Table"];
        }
    }
}
